#!/usr/bin/ruby
# -*- ruby -*-
#
# http://www.reddit.com/r/dailyprogrammer/comments/164zvs/010713_challenge_116_easy_permutation_of_a_string/

require 'set'

def perm(s, curr = "", acc = Set.new)
    s = s.split("") if s.is_a?(String)

    unless s.empty?
        (0 .. s.size - 1).each do |i|
            c = s[i]
            r = s.slice(0, i) + s.slice(i + 1, s.size)

            perm(r, curr + c, acc)
        end
    else
        acc << curr
    end

    return acc
end

ARGV.each do |o|
    puts o + " => " + perm(o).to_a.sort.join(" ")
end
